package cn.springcloud.fix.oauth2.autoconfigure;

import cn.springcloud.fix.oauth2.autoconfigure.authserver.OAuth2AuthorizationServerConfiguration;
import cn.springcloud.fix.oauth2.autoconfigure.client.OAuth2ClientProperties;
import cn.springcloud.fix.oauth2.autoconfigure.client.OAuth2RestOperationsConfiguration;
import cn.springcloud.fix.oauth2.autoconfigure.method.OAuth2MethodSecurityConfiguration;
import cn.springcloud.fix.oauth2.autoconfigure.resource.OAuth2ResourceServerConfiguration;
import cn.springcloud.fix.oauth2.autoconfigure.resource.ResourceServerProperties;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnClass({OAuth2AccessToken.class, WebMvcConfigurer.class})
@AutoConfigureBefore(WebMvcAutoConfiguration.class)
@Import({
        OAuth2AuthorizationServerConfiguration.class,
        OAuth2MethodSecurityConfiguration.class,
        OAuth2ResourceServerConfiguration.class,
        OAuth2RestOperationsConfiguration.class
})
@EnableConfigurationProperties({OAuth2ClientProperties.class})
public class OAuth2AutoConfiguration {

    private final OAuth2ClientProperties oAuth2ClientProperties;

    public OAuth2AutoConfiguration(OAuth2ClientProperties oAuth2ClientProperties) {
        this.oAuth2ClientProperties = oAuth2ClientProperties;
    }

    @Bean
    public ResourceServerProperties resourceServerProperties() {
        return new ResourceServerProperties(this.oAuth2ClientProperties.getClientId(), this.oAuth2ClientProperties.getClientSecret());
    }
}
